package com.gitee.smsdistribution.service;

import com.gitee.smsdistribution.model.DistributionPolicy;
import com.gitee.smsdistribution.repository.DistributionPolicyRepository;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class DistributionPolicyService {

    private final DistributionPolicyRepository distributionPolicyRepository;


    public Page<DistributionPolicy> searchByKeyword(String keyword, int page, int size) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "id"));

        return distributionPolicyRepository.findAllBy(
                "%" + keyword + "%",
                "%" + keyword + "%",
                "%" + keyword + "%",
                pageable);
    }

    public void insertBatch(List<DistributionPolicy> policies) {
        distributionPolicyRepository.saveAll(policies);
    }


    public DistributionPolicy findByName(String name) {
        return distributionPolicyRepository.findByName(name);
    }
}
